package com.qqtech.qquan.mall.controller;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.BizzLevel2Enum;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.ad.enums.AdTypeDictEnum;
import com.qqtech.qquan.ad.model.AdBaseVo;
import com.qqtech.qquan.ad.service.AdBaseService;
import com.qqtech.qquan.community.model.CommunityAddr;
import com.qqtech.qquan.community.model.CommunityAddrVo;
import com.qqtech.qquan.community.model.CommunityBase;
import com.qqtech.qquan.community.service.CommunityAddrService;
import com.qqtech.qquan.community.service.CommunityBaseService;
import com.qqtech.qquan.homemaking.service.HomemakingTypeService;
import com.qqtech.qquan.mall.enums.MallProductStatusDictEnum;
import com.qqtech.qquan.mall.enums.MallShopAuditStatusDictEnum;
import com.qqtech.qquan.mall.enums.MallShopStatusDictEnum;
import com.qqtech.qquan.mall.enums.MallShopStyleDictEnum;
import com.qqtech.qquan.mall.enums.MallShopTypeDictEnum;
import com.qqtech.qquan.mall.enums.MallSortTypeEnum;
import com.qqtech.qquan.mall.model.MallCategoryVo;
import com.qqtech.qquan.mall.model.MallProductQuery;
import com.qqtech.qquan.mall.model.MallProductVo;
import com.qqtech.qquan.mall.model.MallShop;
import com.qqtech.qquan.mall.model.MallShopCategory;
import com.qqtech.qquan.mall.model.MallShopCategoryVo;
import com.qqtech.qquan.mall.model.MallShopQuery;
import com.qqtech.qquan.mall.model.MallShopVo;
import com.qqtech.qquan.mall.service.MallCartService;
import com.qqtech.qquan.mall.service.MallProductService;
import com.qqtech.qquan.mall.service.MallShopCategoryService;
import com.qqtech.qquan.mall.service.MallShopService;
import com.qqtech.qquan.order.enums.OrderBaseDeliveryStatusDictEnum;
import com.qqtech.qquan.order.enums.OrderBaseOrderStatusDictEnum;
import com.qqtech.qquan.order.enums.OrderBasePayStatusDictEnum;
import com.qqtech.qquan.order.enums.OrderBaseReturnPayStatusDictEnum;
import com.qqtech.qquan.order.model.OrderBaseQuery;
import com.qqtech.qquan.order.service.OrderBaseService;
import com.qqtech.qquan.pub.model.PubConcernQuery;
import com.qqtech.qquan.pub.service.PubConcernService;
import com.qqtech.qquan.sys.model.SysArea;
import com.qqtech.qquan.sys.service.SysAreaService;
import com.qqtech.qquan.user.enums.UserAuthAuditStatusDictEnum;
import com.qqtech.qquan.user.model.UserAuthVo;
import com.qqtech.qquan.user.model.UserBase;
import com.qqtech.qquan.user.model.UserBaseQuery;
import com.qqtech.qquan.user.model.UserBaseVo;
import com.qqtech.qquan.user.service.UserAuthService;
import com.qqtech.qquan.user.service.UserBaseService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/mall/shop")
public class MallShopController extends BaseWebController<MallShop, MallShopQuery> {
	@Resource
	private MallShopService mallShopService;
	@Resource
	private CommunityBaseService communityBaseService;
	@Resource
	private CommunityAddrService communityAddrService;
	@Resource
	private SysAreaService sysAreaService;
	@Resource
	private MallShopCategoryService mallShopCategoryService;
	@Resource
	private AdBaseService adBaseService;
	@Resource
	private MallProductService mallProductService;
	@Resource
	private UserBaseService userBaseService;
	@Resource
	private MallCartService mallCartService;
	@Resource
	private HomemakingTypeService homemakingTypeService;
	@Resource
	private OrderBaseService orderBaseService;
	@Resource
	private PubConcernService pubConcernService;
	@Resource
	private UserAuthService userAuthService;

	@Override
	protected BaseService<MallShop> getBaseService() {
		return mallShopService;
	}

	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/hasCreate")
	@ResponseBody
	public Result hasCreate() {
		Result result = new Result();
		MallShopVo mallShop = mallShopService.getByCreatorId(super.getUserId());
		if (mallShop == null) {
			result.put("hasCreate", 0);
		} else {
			result.put("hasCreate", 1);
			result.put("shopId", mallShop.getId());
		}
		return result;
	}

	/**
	 * 创建店铺前判断是否身份认证，并且加入社区
	 * 
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/beforeCreate")
	@ResponseBody
	public Result beforeCreate() {
		Result result = new Result();
		MallShopVo mallShop = mallShopService.getByCreatorId(super.getUserId());
		if (mallShop == null) {
			result.put("hasCreate", 0);
			UserAuthVo userAuthVo = userAuthService.getByUserId(getUserId());
			if (userAuthVo == null) {
				result.put("auditStatus", 0);
				result.put("msg", "未提交身份认证");
			} else if (userAuthVo.getAuditStatus().intValue() == UserAuthAuditStatusDictEnum.NOT_THROUGH.getCode()) {
				result.put("auditStatus", userAuthVo.getAuditStatus().intValue());
				result.put("msg", "身份认证审核不通过，请修改");
			} else if (userAuthVo.getAuditStatus().intValue() == UserAuthAuditStatusDictEnum.WAIT.getCode()) {
				result.put("auditStatus", userAuthVo.getAuditStatus().intValue());
				result.put("msg", "身份认证审核中");
			} else if (userAuthVo.getAuditStatus().intValue() == UserAuthAuditStatusDictEnum.THROUGH.getCode()) {
				result.put("auditStatus", userAuthVo.getAuditStatus().intValue());
				result.put("msg", "身份认证审核通过");
			}
		} else {
			result.put("hasCreate", 1);
			result.put("shopId", mallShop.getId());
		}
		return result;
	}

	/**
	 * 圈商-创建店铺
	 * 
	 * @param mallShopVo
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/doAdd")
	@ResponseBody
	public Result doAdd(@RequestBody MallShopVo mallShopVo) {
		Result result = new Result();
		CommunityBase communityBase = null;
		UserBase curUser = userBaseService.getById(super.getUserId());
		if(curUser.getCommunityId()==null){
			result.addErr("您还没有加入社区！");
		}else{
			mallShopVo.setCommunityId(curUser.getCommunityId());//店铺引用用户加入的社区ID
			
		}
		communityBase = communityBaseService.getById(mallShopVo.getCommunityId());
		if (communityBase == null) {
			result.addErr("社区不存在");
		}else{
			mallShopVo.setExtCommunityIds(String.valueOf(communityBase.getId()));//默认服务本人加入的社区
			mallShopVo.setExtCommunityNames(communityBase.getName());
		}
		String regex = "^[a-z0-9A-Z\u4e00-\u9fa5]+$";
		if (StringUtil.isBlank(mallShopVo.getLogo())) {
			result.addErr("请上传店铺图片");
		}
		if (StringUtil.isBlank(mallShopVo.getName())) {
			result.addErr("请填写店铺名称");
		} else if (mallShopVo.getName().length() > 16) {
			result.addErr("店铺名称不能超过16个字符");
		} else if (!mallShopVo.getName().matches(regex)) {
			result.addErr("名称只能包含包含字母、数字和汉字");
		}
		if (StringUtil.isBlank(mallShopVo.getCategoryId())) {
			result.addErr("请选择主营类目");
		}
		if (StringUtil.isBlank(mallShopVo.getResume())) {
			result.addErr("请填写店铺简介");
		} else if (mallShopVo.getResume().length() > 100) {
			result.addErr("店铺简介不能超过100个字符");
		}
		if (StringUtil.isBlank(mallShopVo.getBusStart())) {
			result.addErr("请选择营业开始时间");
		} else if (StringUtil.isBlank(mallShopVo.getBusEnd())) {
			result.addErr("请选择营业结束时间");
		} else {
			String regexBusTime = "^([01][0-9]|2[0-3]):[0-5][0-9]$";
			if (!mallShopVo.getBusStart().trim().matches(regexBusTime)
					|| !mallShopVo.getBusEnd().trim().matches(regexBusTime)) {
				result.addErr("营业时间格式错误！");
			}
		}
		if (mallShopVo.getStyle() == null) {
			result.addErr("请选择店铺类型");
		} else if (mallShopVo.getStyle().intValue() == MallShopStyleDictEnum.LOCAL.getCode()) {
			if (StringUtil.isBlank(mallShopVo.getLicensePic())) {
				result.addErr("请上传营业执照");
			}
		}
		if (StringUtil.isBlank(mallShopVo.getPhone())) {
			result.addErr("请填写店铺联系方式");
		} else if (!StringUtil.isPhone(mallShopVo.getPhone())) {
			result.addErr("联系方式请输入正确的手机号");
		}
		if (StringUtil.isBlank(mallShopVo.getSendDeliveryType())) {
			result.addErr("请选择配送类型");
		}
		if (result.checkForm()) {
			Timestamp now = TimeUtil.now();
			MallShop newShop = new MallShop();
			newShop.setType(MallShopTypeDictEnum.QQMALLSHOP.getCode());
			newShop.setStyle(mallShopVo.getStyle());
			newShop.setNewAt(now);
			newShop.setCategoryId(mallShopVo.getCategoryId());
			newShop.setCreatorId(super.getUserId());
			newShop.setAuditStatus(MallShopAuditStatusDictEnum.PENDING.getCode());
			newShop.setStatus(MallShopStatusDictEnum.NORMAL.getCode());
			newShop.setName(mallShopVo.getName().trim());
			newShop.setLogo(mallShopVo.getLogo());
			newShop.setResume(mallShopVo.getResume());
			newShop.setPhone(mallShopVo.getPhone().trim());
			newShop.setCommunityId(mallShopVo.getCommunityId());
			newShop.setSendDeliveryType(mallShopVo.getSendDeliveryType());
			
			if (mallShopVo.getStyle().intValue() == MallShopStyleDictEnum.LOCAL.getCode()) {
				newShop.setLicensePic(mallShopVo.getLicensePic());
			} else {
				newShop.setLicensePic("");
			}
			newShop.setResumeImg("");
			newShop.setFreight(BigDecimal.ZERO);
			if (mallShopVo.getFreeFreight() != null) {
				newShop.setFreeFreight(mallShopVo.getFreeFreight());
			} else {
				newShop.setFreeFreight(BigDecimal.ZERO);
			}
			newShop.setRate(BigDecimal.ONE);
			SysArea city = sysAreaService.getById(communityBase.getCityId());
			newShop.setProvinceId(city.getParentId());
			newShop.setCityId(communityBase.getCityId());
			newShop.setRegionId(communityBase.getDistrictId());
			//newShop.setX(communityBase.getX());
			//newShop.setY(communityBase.getY());
			newShop.setX(mallShopVo.getX());
			newShop.setY(mallShopVo.getY());
			/*
			不再使用社区的地址，而是手工填写的地址
			newShop.setAddress(mallShopVo.getHomeNum().trim());
			List<CommunityAddrVo> pCommunityAddr = communityAddrService
					.queryListByChildren(mallShopVo.getCommunityAddrId(), mallShopVo.getCommunityId());
			for (CommunityAddrVo t : pCommunityAddr) {
				newShop.setAddress(t.getName().trim() + newShop.getAddress());
			}
			*/
			
			newShop.setAddress(mallShopVo.getAddress());
			String regexBusTime = "^([01][0-9]|2[0-3]):[0-5][0-9]$";
			if (mallShopVo.getBusStart().trim().matches(regexBusTime)) {
				newShop.setBusStart(mallShopVo.getBusStart().trim());
			} else {
				newShop.setBusStart("00:00");
			}
			if (mallShopVo.getBusEnd().trim().matches(regexBusTime)) {
				newShop.setBusEnd(mallShopVo.getBusEnd().trim());
			} else {
				newShop.setBusEnd("23:59");
			}
			newShop.setPayType("2");// 只有在线支付
			newShop.setHasRecommend(YesNoEnum.NO.getCode());
			if (mallShopVo.getDeliveryValue() == null || mallShopVo.getDeliveryValue().compareTo(BigDecimal.ZERO) < 1) {
				newShop.setDeliveryValue(BigDecimal.ZERO);
			} else {
				newShop.setDeliveryValue(mallShopVo.getDeliveryValue());
			}
			if (mallShopVo.getFreight() == null || mallShopVo.getFreight().compareTo(BigDecimal.ZERO) < 1) {
				newShop.setFreight(BigDecimal.ZERO);
			} else {
				newShop.setFreight(mallShopVo.getFreight());
			}
			newShop.setSaleOrderCount(0);
			newShop.setIsUseQuanPay(YesNoEnum.NO.getCode());
			newShop.setQuanPayRate(BigDecimal.ZERO);
			newShop.setQqQuanPayRate(BigDecimal.ZERO);
			newShop.setExtCommunityIds(mallShopVo.getExtCommunityIds());
			newShop.setExtCommunityNames(mallShopVo.getExtCommunityNames());
			try {
				mallShopService.handleSave(newShop);
			} catch (Exception e) {
				result.addErr("您已经创建过店铺了");
			}
		}
		return result;
	}

	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/updateName")
	@ResponseBody
	public Result updateName(@RequestBody MallShopVo mallShopVo) {
		Result result = new Result();
		MallShopVo old = null;
		if (mallShopVo == null) {
			result.addErr("参数不能为空");
		} else if (mallShopVo.getId() == null) {
			result.addErr("参数id不能为空");
		} else {
			old = mallShopService.getById(mallShopVo.getId());
			if (old == null) {
				result.addErr("店铺不存在");
			} else {
				String regex = "^[a-z0-9A-Z\u4e00-\u9fa5]+$";
				if (StringUtil.isBlank(mallShopVo.getName())) {
					result.addErr("请输入店铺名称");
				} else if (mallShopVo.getName().length() > 32) {
					result.addErr("店铺名称不能超过32个字符");
				} else if (!mallShopVo.getName().matches(regex)) {
					result.addErr("名称只能包含包含字母、数字和汉字");
				}
			}
		}
		if (result.checkForm()) {
			MallShop entity = new MallShop();
			entity.setId(mallShopVo.getId());
			entity.setName(mallShopVo.getName().trim());
			if (old.getAuditStatus().intValue() == MallShopAuditStatusDictEnum.NOTPASS.getCode()) {
				entity.setAuditStatus(MallShopAuditStatusDictEnum.PENDING.getCode());
			}
			mallShopService.updateById(entity);
			result.put("mallShop", mallShopVo);
		}
		return result;
	}

	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/updateLogo")
	@ResponseBody
	public Result updateLogo(@RequestBody MallShopVo mallShopVo) {
		Result result = new Result();
		MallShopVo old = null;
		if (mallShopVo == null) {
			result.addErr("参数不能为空");
		} else if (mallShopVo.getId() == null) {
			result.addErr("参数id不能为空");
		} else {
			old = mallShopService.getById(mallShopVo.getId());
			if (old == null) {
				result.addErr("店铺不存在");
			} else {
				if (StringUtil.isBlank(mallShopVo.getLogo())) {
					result.addErr("请上传店铺图片");
				}
			}
		}
		if (result.checkForm()) {
			MallShop entity = new MallShop();
			entity.setId(mallShopVo.getId());
			entity.setLogo(mallShopVo.getLogo().trim());
			if (old.getAuditStatus().intValue() == MallShopAuditStatusDictEnum.NOTPASS.getCode()) {
				entity.setAuditStatus(MallShopAuditStatusDictEnum.PENDING.getCode());
			}
			mallShopService.updateById(entity);
			result.put("mallShop", mallShopVo);
		}
		return result;
	}

	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/updateResume")
	@ResponseBody
	public Result updateResume(@RequestBody MallShopVo mallShopVo) {
		Result result = new Result();
		MallShopVo old = null;
		if (mallShopVo == null) {
			result.addErr("参数不能为空");
		} else if (mallShopVo.getId() == null) {
			result.addErr("参数id不能为空");
		} else {
			old = mallShopService.getById(mallShopVo.getId());
			if (old == null) {
				result.addErr("店铺不存在");
			} else {
				if (StringUtil.isBlank(mallShopVo.getResume())) {
					result.addErr("请填写店铺简介");
				} else if (mallShopVo.getResume().length() > 100) {
					result.addErr("店铺简介不能超过100个字符");
				}
			}
		}
		if (result.checkForm()) {
			MallShop entity = new MallShop();
			entity.setId(mallShopVo.getId());
			entity.setResume(mallShopVo.getResume());
			if (old.getAuditStatus().intValue() == MallShopAuditStatusDictEnum.NOTPASS.getCode()) {
				entity.setAuditStatus(MallShopAuditStatusDictEnum.PENDING.getCode());
			}
			mallShopService.updateById(entity);
			result.put("mallShop", mallShopVo);
		}
		return result;
	}

	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/updatePhone")
	@ResponseBody
	public Result updatePhone(@RequestBody MallShopVo mallShopVo) {
		Result result = new Result();
		MallShopVo old = null;
		if (mallShopVo == null) {
			result.addErr("参数不能为空");
		} else if (mallShopVo.getId() == null) {
			result.addErr("参数id不能为空");
		} else {
			old = mallShopService.getById(mallShopVo.getId());
			if (old == null) {
				result.addErr("店铺不存在");
			} else {
				if (StringUtil.isBlank(mallShopVo.getPhone())) {
					result.addErr("请填写店铺联系方式");
				} else if (!StringUtil.isPhone(mallShopVo.getPhone())) {
					result.addErr("手机号格式不正确");
				}
			}
		}
		if (result.checkForm()) {
			MallShop entity = new MallShop();
			entity.setId(mallShopVo.getId());
			entity.setPhone(mallShopVo.getPhone().trim());
			if (old.getAuditStatus().intValue() == MallShopAuditStatusDictEnum.NOTPASS.getCode()) {
				entity.setAuditStatus(MallShopAuditStatusDictEnum.PENDING.getCode());
			}
			mallShopService.updateById(entity);
			result.put("mallShop", mallShopVo);
		}
		return result;
	}

	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/updatePayType")
	@ResponseBody
	public Result updatePayType(@RequestBody MallShopVo mallShopVo) {
		Result result = new Result();
		MallShopVo old = null;
		if (mallShopVo == null) {
			result.addErr("参数不能为空");
		} else if (mallShopVo.getId() == null) {
			result.addErr("参数id不能为空");
		} else {
			old = mallShopService.getById(mallShopVo.getId());
			if (old == null) {
				result.addErr("店铺不存在");
			} else {
				if (StringUtil.isBlank(mallShopVo.getPayType())) {
					result.addErr("请选择支付方式");
				}
			}
		}
		if (result.checkForm()) {
			MallShop entity = new MallShop();
			entity.setId(mallShopVo.getId());
			entity.setPayType(mallShopVo.getPayType().trim());
			if (old.getAuditStatus().intValue() == MallShopAuditStatusDictEnum.NOTPASS.getCode()) {
				entity.setAuditStatus(MallShopAuditStatusDictEnum.PENDING.getCode());
			}
			mallShopService.updateById(entity);
			result.put("mallShop", mallShopVo);
		}
		return result;
	}

	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/updateSendDeliveryType")
	@ResponseBody
	public Result updateSendDeliveryType(@RequestBody MallShopVo mallShopVo) {
		Result result = new Result();
		MallShopVo old = null;
		if (mallShopVo == null) {
			result.addErr("参数不能为空");
		} else if (mallShopVo.getId() == null) {
			result.addErr("参数id不能为空");
		} else {
			old = mallShopService.getById(mallShopVo.getId());
			if (old == null) {
				result.addErr("店铺不存在");
			} else {
				if (StringUtil.isBlank(mallShopVo.getSendDeliveryType())) {
					result.addErr("请选择配送类型");
				}else{
					if(old.getStyle().intValue() == MallShopStyleDictEnum.QQSTORE.getCode()
							&& mallShopVo.getSendDeliveryType().indexOf("2") > -1){
						result.addErr("便利店无法使用商家配送");
					}
				}
			}
		}
		if (result.checkForm()) {
			MallShop entity = new MallShop();
			entity.setId(mallShopVo.getId());
			entity.setSendDeliveryType(mallShopVo.getSendDeliveryType().trim());
			if (old.getAuditStatus().intValue() == MallShopAuditStatusDictEnum.NOTPASS.getCode()) {
				entity.setAuditStatus(MallShopAuditStatusDictEnum.PENDING.getCode());
			}
			mallShopService.updateById(entity);
			result.put("mallShop", mallShopVo);
		}
		return result;
	}

	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/updateStatus")
	@ResponseBody
	public Result updateStatus(@RequestBody MallShopVo mallShopVo) {
		Result result = new Result();
		MallShopVo old = null;
		if (mallShopVo == null) {
			result.addErr("参数不能为空");
		} else if (mallShopVo.getId() == null) {
			result.addErr("参数id不能为空");
		} else {
			old = mallShopService.getById(mallShopVo.getId());
			if (old == null) {
				result.addErr("店铺不存在");
			} else {
				if (mallShopVo.getStatus() == null) {
					result.addErr("请选择营业状态");
				}
			}
		}
		if (result.checkForm()) {
			MallShop entity = new MallShop();
			entity.setId(mallShopVo.getId());
			entity.setStatus(mallShopVo.getStatus());
			if (old.getAuditStatus().intValue() == MallShopAuditStatusDictEnum.NOTPASS.getCode()) {
				entity.setAuditStatus(MallShopAuditStatusDictEnum.PENDING.getCode());
			}
			mallShopService.updateById(entity);
			result.put("mallShop", mallShopVo);
		}
		return result;
	}

	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/updateFreeFreight")
	@ResponseBody
	public Result updateFreeFreight(@RequestBody MallShopVo mallShopVo) {
		Result result = new Result();
		MallShopVo old = null;
		if (mallShopVo == null) {
			result.addErr("参数不能为空");
		} else if (mallShopVo.getId() == null) {
			result.addErr("参数id不能为空");
		} else {
			old = mallShopService.getById(mallShopVo.getId());
			if (old == null) {
				result.addErr("店铺不存在");
			} else {
				if (mallShopVo.getFreeFreight() == null) {
					result.addErr("请填写免运费金额");
				}
			}
		}
		if (result.checkForm()) {
			MallShop entity = new MallShop();
			entity.setId(mallShopVo.getId());
			entity.setFreeFreight(mallShopVo.getFreeFreight());
			if (old.getAuditStatus().intValue() == MallShopAuditStatusDictEnum.NOTPASS.getCode()) {
				entity.setAuditStatus(MallShopAuditStatusDictEnum.PENDING.getCode());
			}
			mallShopService.updateById(entity);
			result.put("mallShop", mallShopVo);
		}
		return result;
	}

	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/updateDeliveryValue")
	@ResponseBody
	public Result updateDeliveryValue(@RequestBody MallShopVo mallShopVo) {
		Result result = new Result();
		MallShopVo old = null;
		if (mallShopVo == null) {
			result.addErr("参数不能为空");
		} else if (mallShopVo.getId() == null) {
			result.addErr("参数id不能为空");
		} else {
			old = mallShopService.getById(mallShopVo.getId());
			if (old == null) {
				result.addErr("店铺不存在");
			} else {
				if (mallShopVo.getDeliveryValue() == null) {
					result.addErr("请输入起送金额");
				}
			}
		}
		if (result.checkForm()) {
			MallShop entity = new MallShop();
			entity.setId(mallShopVo.getId());
			if (mallShopVo.getDeliveryValue() == null || mallShopVo.getDeliveryValue().compareTo(BigDecimal.ZERO) < 1) {
				entity.setDeliveryValue(BigDecimal.ZERO);
			} else {
				entity.setDeliveryValue(mallShopVo.getDeliveryValue());
			}
			if (old.getAuditStatus().intValue() == MallShopAuditStatusDictEnum.NOTPASS.getCode()) {
				entity.setAuditStatus(MallShopAuditStatusDictEnum.PENDING.getCode());
			}
			mallShopService.updateById(entity);
			result.put("mallShop", mallShopVo);
		}
		return result;
	}
	
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/updateFreight")
	@ResponseBody
	public Result updateFreight(@RequestBody MallShopVo mallShopVo) {
		Result result = new Result();
		MallShopVo old = null;
		if (mallShopVo == null) {
			result.addErr("参数不能为空");
		} else if (mallShopVo.getId() == null) {
			result.addErr("参数id不能为空");
		} else {
			old = mallShopService.getById(mallShopVo.getId());
			if (old == null) {
				result.addErr("店铺不存在");
			} else {
				if (mallShopVo.getFreight() == null) {
					result.addErr("请输入配送费");
				}
			}
		}
		if (result.checkForm()) {
			MallShop entity = new MallShop();
			entity.setId(mallShopVo.getId());
			if (mallShopVo.getFreight() == null || mallShopVo.getFreight().compareTo(BigDecimal.ZERO) < 1) {
				entity.setFreight(BigDecimal.ZERO);
			} else {
				entity.setFreight(mallShopVo.getFreight());
			}
			if (old.getAuditStatus().intValue() == MallShopAuditStatusDictEnum.NOTPASS.getCode()) {
				entity.setAuditStatus(MallShopAuditStatusDictEnum.PENDING.getCode());
			}
			mallShopService.updateById(entity);
			result.put("mallShop", mallShopVo);
		}
		return result;
	}

	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/updateCategoryId")
	@ResponseBody
	public Result updateCategoryId(@RequestBody MallShopVo mallShopVo) {
		Result result = new Result();
		MallShopVo old = null;
		if (mallShopVo == null) {
			result.addErr("参数不能为空");
		} else if (mallShopVo.getId() == null) {
			result.addErr("参数id不能为空");
		} else {
			old = mallShopService.getById(mallShopVo.getId());
			if (old == null) {
				result.addErr("店铺不存在");
			} else {
				if (StringUtil.isBlank(mallShopVo.getCategoryId())) {
					result.addErr("请选择主营类目");
				}
			}
		}
		if (result.checkForm()) {
			MallShop entity = new MallShop();
			entity.setId(mallShopVo.getId());
			entity.setCategoryId(mallShopVo.getCategoryId().trim());
			if (old.getAuditStatus().intValue() == MallShopAuditStatusDictEnum.NOTPASS.getCode()) {
				entity.setAuditStatus(MallShopAuditStatusDictEnum.PENDING.getCode());
			}
			mallShopService.updateById(entity);
			MallShopCategory mallShopCategory = mallShopCategoryService.getByCategoryId(mallShopVo.getCategoryId());
			if (mallShopCategory != null) {
				String parentCategoryStr = "";
				if (mallShopCategory.getName().equals("其他") || mallShopCategory.getName().equals("全部")) {
					MallShopCategory parentCategory = mallShopCategoryService
							.getByCategoryId(mallShopCategory.getParentId());
					if (parentCategory != null) {
						parentCategoryStr = parentCategory.getName() + "-";
					}
				}
				mallShopVo.setCategoryName(parentCategoryStr + mallShopCategory.getName());
			}
			result.put("mallShop", mallShopVo);
		}
		return result;
	}

	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/updateAddress")
	@ResponseBody
	public Result updateAddress(@RequestBody MallShopVo mallShopVo) {
		Result result = new Result();
		MallShopVo old = null;
		CommunityBase communityBase = null;
		CommunityAddr communityAddr = null;
		if (mallShopVo == null) {
			result.addErr("参数不能为空");
		} else if (mallShopVo.getId() == null) {
			result.addErr("参数id不能为空");
		} else {
			old = mallShopService.getById(mallShopVo.getId());
			if (old == null) {
				result.addErr("店铺不存在");
			} else {
				if (mallShopVo.getCommunityId() == null) {
					result.addErr("请选择店铺的社区");
				} else {
					communityBase = communityBaseService.getById(mallShopVo.getCommunityId());
					if (communityBase == null) {
						result.addErr("社区不存在");
					} else {
						if (mallShopVo.getCommunityAddrId() == null) {
							result.addErr("请选择社区层级");
						} else {
							communityAddr = communityAddrService.getById(mallShopVo.getCommunityAddrId());
							if (communityAddr == null) {
								result.addErr("社区层级不存在");
							} else {
								List<CommunityAddrVo> list = communityAddrService
										.queryListByParentId(communityAddr.getId(), mallShopVo.getCommunityId());
								if (list != null && !list.isEmpty()) {
									result.addErr("社区层级没有选完");
								}
							}
						}
					}
				}
				if (StringUtil.isBlank(mallShopVo.getHomeNum())) {
					result.addErr("请填写门牌号");
				} else if (mallShopVo.getHomeNum().length() > 10) {
					result.addErr("门牌号不能超过10个字符");
				}
			}
		}
		if (result.checkForm()) {
			MallShop entity = new MallShop();
			entity.setId(mallShopVo.getId());
			SysArea city = sysAreaService.getById(communityBase.getCityId());
			entity.setProvinceId(city.getParentId());
			entity.setCityId(communityBase.getCityId());
			entity.setRegionId(communityBase.getDistrictId());
			entity.setX(communityBase.getX());
			entity.setY(communityBase.getY());
			entity.setAddress(mallShopVo.getHomeNum().trim());
			List<CommunityAddrVo> pCommunityAddr = communityAddrService
					.queryListByChildren(mallShopVo.getCommunityAddrId(), mallShopVo.getCommunityId());
			for (CommunityAddrVo t : pCommunityAddr) {
				entity.setAddress(t.getName().trim() + entity.getAddress());
			}
			if (old.getAuditStatus().intValue() == MallShopAuditStatusDictEnum.NOTPASS.getCode()) {
				entity.setAuditStatus(MallShopAuditStatusDictEnum.PENDING.getCode());
			}
			mallShopService.updateById(entity);
			mallShopVo.setAddress(communityBase.getName() + entity.getAddress());
			result.put("mallShop", mallShopVo);
		}
		return result;
	}
	
	/**
	 * 修改店铺地址，直接地图定位X，Y，可手改地址
	 * @param mallShopVo
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/updateAddressV1")
	@ResponseBody
	public Result updateAddressV1(@RequestBody MallShopVo mallShopVo) {
		Result result = new Result();
		MallShopVo old = null;
		if (mallShopVo == null) {
			result.addErr("参数不能为空");
		} else if (mallShopVo.getId() == null) {
			result.addErr("参数id不能为空");
		} else {
			old = mallShopService.getById(mallShopVo.getId());
			if (old == null) {
				result.addErr("店铺不存在");
			} else {
				if (mallShopVo.getX() == null || mallShopVo.getY()==null) {
					result.addErr("请在地图上标记位置");
				} else if(StringUtil.isBlank(mallShopVo.getAddress())){
					result.addErr("请填写详细地址");
				}
			}
		}
		if (result.checkForm()) {
			MallShop entity = new MallShop();
			entity.setId(mallShopVo.getId());
			entity.setX(mallShopVo.getX());
			entity.setY(mallShopVo.getY());
			entity.setAddress(mallShopVo.getAddress());
			
			if (old.getAuditStatus().intValue() == MallShopAuditStatusDictEnum.NOTPASS.getCode()) {
				entity.setAuditStatus(MallShopAuditStatusDictEnum.PENDING.getCode());
			}
			mallShopService.updateById(entity);
			result.put("mallShop", mallShopVo);
		}
		return result;
	}

	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/updateLicensePic")
	@ResponseBody
	public Result updateLicensePic(@RequestBody MallShopVo mallShopVo) {
		Result result = new Result();
		MallShopVo mallShop = null;
		if (mallShopVo == null) {
			result.addErr("参数不能为空");
		} else if (mallShopVo.getId() == null) {
			result.addErr("参数id不能为空");
		} else {
			if (StringUtil.isBlank(mallShopVo.getLicensePic())) {
				result.addErr("请上传营业执照");
			} else {
				mallShop = mallShopService.getById(mallShopVo.getId());
				if (mallShop == null) {
					result.addErr("店铺不存在");
				} else if (mallShop.getStyle().intValue() != MallShopStyleDictEnum.LOCAL.getCode()) {
					result.addErr("社区店铺才需要营业执照");
				} else if (mallShop.getAuditStatus().intValue() == MallShopAuditStatusDictEnum.NORMAL.getCode()) {
					result.addErr("店铺已审核通过，不能更换营业执照");
				}
			}
		}
		if (result.checkForm()) {
			MallShop entity = new MallShop();
			entity.setId(mallShopVo.getId());
			entity.setLicensePic(mallShopVo.getLicensePic().trim());
			if (mallShop.getAuditStatus().intValue() == MallShopAuditStatusDictEnum.NOTPASS.getCode()) {
				entity.setAuditStatus(MallShopAuditStatusDictEnum.PENDING.getCode());
			}
			mallShopService.updateById(entity);
			result.put("mallShop", mallShopVo);
		}
		return result;
	}

	/**
	 * 更新营业时间
	 * 
	 * @param mallShopVo
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/updateBusTime")
	@ResponseBody
	public Result updateBusTime(@RequestBody MallShopVo mallShopVo) {
		Result result = new Result();
		MallShopVo old = null;
		if (mallShopVo == null) {
			result.addErr("参数不能为空");
		} else if (mallShopVo.getId() == null) {
			result.addErr("参数id不能为空");
		} else {
			old = mallShopService.getById(mallShopVo.getId());
			if (old == null) {
				result.addErr("店铺不存在");
			} else {
				if (StringUtil.isBlank(mallShopVo.getBusStart())) {
					result.addErr("请选择开始时间");
				} else if (StringUtil.isBlank(mallShopVo.getBusEnd())) {
					result.addErr("请选择结束时间");
				} else {
					String regexBusTime = "^([01][0-9]|2[0-3]):[0-5][0-9]$";
					/*
					 * if (mallShopVo.getBusStart().trim().matches(regexBusTime)
					 * && mallShopVo.getBusEnd().trim().matches(regexBusTime)) {
					 * Timestamp now = TimeUtil.now(); String date =
					 * TimeUtil.getStringTimeByFormat(now,
					 * TimeUtil.DATE_FORMAT); Timestamp busStartAt =
					 * TimeUtil.getTimeByFormat(date + " " +
					 * mallShopVo.getBusStart() + ":00",
					 * TimeUtil.DATE_TIME_FORMAT); Timestamp busEndAt =
					 * TimeUtil.getTimeByFormat(date + " " +
					 * mallShopVo.getBusEnd() + ":00",
					 * TimeUtil.DATE_TIME_FORMAT); if
					 * (busStartAt.compareTo(busEndAt) > 0) {
					 * result.addErr("开始时间要大于结束时间！"); } }
					 */
					if (!mallShopVo.getBusStart().trim().matches(regexBusTime)
							|| !mallShopVo.getBusEnd().trim().matches(regexBusTime)) {
						result.addErr("营业时间格式错误！");
					}
				}
			}
		}
		if (result.checkForm()) {
			MallShop entity = new MallShop();
			entity.setId(mallShopVo.getId());
			String regexBusTime = "^([01][0-9]|2[0-3]):[0-5][0-9]$";
			if (mallShopVo.getBusStart().trim().matches(regexBusTime)) {
				entity.setBusStart(mallShopVo.getBusStart().trim());
			} else {
				entity.setBusStart("00:00");
			}
			if (mallShopVo.getBusEnd().trim().matches(regexBusTime)) {
				entity.setBusEnd(mallShopVo.getBusEnd().trim());
			} else {
				entity.setBusEnd("23:59");
			}
			if (old.getAuditStatus().intValue() == MallShopAuditStatusDictEnum.NOTPASS.getCode()) {
				entity.setAuditStatus(MallShopAuditStatusDictEnum.PENDING.getCode());
			}
			mallShopService.updateById(entity);
			result.put("mallShop", mallShopVo);
		}
		return result;
	}
	
	/**
	 * 修改服务社区
	 * @param mallShopVo
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/updateExtCommunity")
	@ResponseBody
	public Result updateExtCommunity(@RequestBody MallShopVo mallShopVo) {
		Result result = new Result();
		MallShopVo mallShop = null;
		if (mallShopVo == null) {
			result.addErr("参数不能为空");
		} else if (mallShopVo.getId() == null) {
			result.addErr("参数id不能为空");
		} else {
			if (StringUtil.isBlank(mallShopVo.getExtCommunityIds())) {
				result.addErr("请选择服务社区");
			} else {
				mallShop = mallShopService.getById(mallShopVo.getId());
				if (mallShop == null) {
					result.addErr("店铺不存在");
				}
			}
		}
		if (result.checkForm()) {
			MallShop entity = new MallShop();
			entity.setId(mallShopVo.getId());
			entity.setExtCommunityIds(mallShopVo.getExtCommunityIds());
			entity.setExtCommunityNames(mallShopVo.getExtCommunityNames());
			if (mallShop.getAuditStatus().intValue() == MallShopAuditStatusDictEnum.NOTPASS.getCode()) {
				entity.setAuditStatus(MallShopAuditStatusDictEnum.PENDING.getCode());
			}
			mallShopService.updateById(entity);
			result.put("mallShop", mallShopVo);
		}
		return result;
	}

	/**
	 * 我的店铺
	 * 
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/myShop")
	@ResponseBody
	public Result myShop() {
		Result result = new Result();
		MallShopVo mallShop = mallShopService.getByCreatorId(super.getUserId());
		if (mallShop == null) {
			result.addErr("店铺不存在");
			return result;
		}

		mallShop.setTotalIncome(orderBaseService.queryTotalPriceByShop(mallShop.getId()));// 收益总计

		OrderBaseQuery orderBaseQuery = new OrderBaseQuery();
		orderBaseQuery.setShopId(mallShop.getId());
		orderBaseQuery.setSellerId(super.getUserId());
		orderBaseQuery.setOrderStatus(OrderBaseOrderStatusDictEnum.UNCOMPLETED.getCode());// 未完成
		// 待退款
		orderBaseQuery.setPayStatus(OrderBasePayStatusDictEnum.PAYED.getCode());// 已付款
		orderBaseQuery.setReturnPayStatus(OrderBaseReturnPayStatusDictEnum.RETURNING.getCode());// 退款中
		int waitRefundCount = orderBaseService.queryCount(orderBaseQuery);
		mallShop.setWaitRefundCount(waitRefundCount);
		// 待发货
		orderBaseQuery.setPayStatus(OrderBasePayStatusDictEnum.PAYED.getCode());// 已付款
		orderBaseQuery.setDeliveryStatus(OrderBaseDeliveryStatusDictEnum.UNSEND.getCode());// 未发货
		orderBaseQuery.setReturnPayStatus(null);
		int waitSendCount = orderBaseService.queryCount(orderBaseQuery);
		mallShop.setWaitSendCount(waitSendCount);
		// 待收货
		orderBaseQuery.setDeliveryStatus(OrderBaseDeliveryStatusDictEnum.SENDED.getCode());// 商家已发货
		int waitReceiptCount = orderBaseService.queryCount(orderBaseQuery);
		mallShop.setWaitReceiptCount(waitReceiptCount);
		//今日有效订单
		orderBaseQuery.setDeliveryStatus(null);
		orderBaseQuery.setOrderStatus(OrderBaseOrderStatusDictEnum.COMPLETED.getCode());// 已完成
		orderBaseQuery.setOrderStatusIsToday(YesNoEnum.YES.getCode());
		int todayCompletedCount=orderBaseService.queryCount(orderBaseQuery);
		mallShop.setTodayCompletedCount(todayCompletedCount);
		//今日有效订单金额总和
		BigDecimal totalTodayCompleted=orderBaseService.getSumSellerTotal(orderBaseQuery);
		mallShop.setTotalTodayCompleted(totalTodayCompleted);
		result.put("mallShop", mallShop);
		return result;
	}

	@RequestMapping(value = "/detail")
	@ResponseBody
	public Result detail(@RequestBody MallShopVo mallShopVo) {
		Result result = new Result();
		MallShopVo mallShop = mallShopService.getByCreatorId(super.getUserId());
		/*
		CommunityBase communityBase = communityBaseService.getById(mallShop.getCommunityId());
		if (communityBase != null) {
			mallShop.setAddress(communityBase.getName() + mallShop.getAddress());
		}*/
		MallShopCategory mallShopCategory = mallShopCategoryService.getByCategoryId(mallShop.getCategoryId());
		if (mallShopCategory != null) {
			String parentCategoryStr = "";
			if (mallShopCategory.getName().equals("其他") || mallShopCategory.getName().equals("全部")) {
				MallShopCategory parentCategory = mallShopCategoryService
						.getByCategoryId(mallShopCategory.getParentId());
				if (parentCategory != null) {
					parentCategoryStr = parentCategory.getName() + "-";
				}
			}
			mallShop.setCategoryName(parentCategoryStr + mallShopCategory.getName());
		}
		result.put("mallShop", mallShop);
		return result;
	}

	@RequestMapping(value = "/detailByShopId")
	@ResponseBody
	public Result detailByShopId(@RequestBody MallShopVo mallShopVo) {
		Result result = new Result();
		MallShopVo mallShop = mallShopService.getById(mallShopVo.getId());
		if (mallShop == null) {
			result.addErr("店铺不存在");
			return result;
		}
		UserBaseVo userBase = userBaseService.getById(mallShop.getCreatorId());
		if (userBase != null) {
			mallShop.setUserNick(userBase.getNick());
		}
		CommunityBase communityBase = communityBaseService.getById(mallShop.getCommunityId());
		if (communityBase != null) {
			mallShop.setAddress(communityBase.getName() + mallShop.getAddress());
		}
		MallShopCategory mallShopCategory = mallShopCategoryService.getByCategoryId(mallShop.getCategoryId());
		if (mallShopCategory != null) {
			String parentCategoryStr = "";
			if (mallShopCategory.getName().equals("其他") || mallShopCategory.getName().equals("全部")) {
				MallShopCategory parentCategory = mallShopCategoryService
						.getByCategoryId(mallShopCategory.getParentId());
				if (parentCategory != null) {
					parentCategoryStr = parentCategory.getName() + "-";
				}
			}
			mallShop.setCategoryName(parentCategoryStr + mallShopCategory.getName());
		}
		if (super.getUserId() == null) {
			mallShop.setHasCollect(false);
		} else {
			mallShop.setHasCollect(pubConcernService.isConcernedByUser(mallShop.getId().intValue(),
					BizzLevel2Enum.QSHOP_SHOP.getCode(), super.getUserId()));
		}
		result.put("mallShop", mallShop);
		return result;
	}

	/**
	 * app “发现”页面数据
	 * 
	 * @return
	 */
	@RequestMapping(value = "/discover")
	@ResponseBody
	public Result discover(@RequestBody MallShopQuery query) {
		Result result = new Result();
		// 广告
		List<AdBaseVo> adList = adBaseService.getAdListByType(AdTypeDictEnum.DISCOVER,query.getCommunityId());
		// 首页推荐8个商品
		MallProductQuery productQuery = new MallProductQuery();
		productQuery.setCommunityId(query.getCommunityId());
		productQuery.setDiscoverRecommend(YesNoEnum.YES.getCode());
		productQuery.setStatus(MallProductStatusDictEnum.ONSHELF.getCode());
		productQuery.setShopAuditStatus(MallShopAuditStatusDictEnum.NORMAL.getCode());
		productQuery.setShopType(MallShopTypeDictEnum.QQMALLSHOP.getCode());
		Pageable pageKit = new PageKit(1, 8, new PageOrder(Direction.DESC, "mp.id"));
		Page<MallProductVo> productPage = mallProductService.queryPageByShop(productQuery, pageKit);
		List<MallProductVo> productList = new ArrayList<MallProductVo>();
		if (productPage != null && productPage.getContent() != null) {
			productList = new ArrayList(productPage.getContent());
			if (productList != null && productList.size() < 8) {
				MallProductQuery newProductQuery = new MallProductQuery();
				newProductQuery.setCommunityId(query.getCommunityId());
				newProductQuery.setDiscoverRecommend(YesNoEnum.NO.getCode());
				newProductQuery.setStatus(MallProductStatusDictEnum.ONSHELF.getCode());
				newProductQuery.setShopAuditStatus(MallShopAuditStatusDictEnum.NORMAL.getCode());
				newProductQuery.setShopType(MallShopTypeDictEnum.QQMALLSHOP.getCode());
				pageKit = new PageKit(1, 8 - productList.size(), new PageOrder(Direction.DESC, "mp.id"));
				Page<MallProductVo> newProductPage = mallProductService.queryPageByShop(newProductQuery, pageKit);
				if (newProductPage != null) {
					List<MallProductVo> newProductList = new ArrayList<MallProductVo>();
					newProductList = new ArrayList(newProductPage.getContent());
					if (newProductList != null) {
						productList.addAll(newProductList);
					}
				}
			}
		}
		// 邻里店铺，推荐的6个，不够则查最新的补充
		List<MallShopVo> neiShopList = mallShopService.listByHasRecommend(query.getCommunityId(),
				MallShopStyleDictEnum.VIRTUAL.getCode(), 6);
		if (neiShopList == null || neiShopList.isEmpty()) {
			neiShopList = new ArrayList<MallShopVo>();
		}
		if (neiShopList.size() < 6) {
			MallShopQuery shopQuery = new MallShopQuery();
			shopQuery.setCommunityId(query.getCommunityId());
			shopQuery.setType(MallShopTypeDictEnum.QQMALLSHOP.getCode());
			shopQuery.setStyle(MallShopStyleDictEnum.VIRTUAL.getCode());
			shopQuery.setAuditStatus(MallShopAuditStatusDictEnum.NORMAL.getCode());
			shopQuery.setHasRecommend(YesNoEnum.NO.getCode());
			pageKit = new PageKit(1, 6 - neiShopList.size(), new PageOrder(Direction.DESC, "id"));
			List<MallShopVo> temList = mallShopService.queryList(shopQuery, pageKit);
			if (temList != null && !temList.isEmpty()) {
				neiShopList.addAll(temList);
			}
		}
		if (!neiShopList.isEmpty()) {
			PubConcernQuery pubConcernQuery = new PubConcernQuery();
			for (MallShopVo t : neiShopList) {
				pubConcernQuery.setBizzId(t.getId());
				pubConcernQuery.setBizzType(BizzLevel2Enum.QSHOP_SHOP.getCode());
				int c = pubConcernService.queryCount(pubConcernQuery);
				t.setCollectCount(c);
				t.setRemarks1(c + "人收藏了店铺");
			}
		}
		// 周边店铺列表
		// 先查出我收藏的店铺
		Integer loginUserId = super.getUserId();
		List<MallShopVo> collectShops = null;
		if (loginUserId != null) {
			collectShops = mallShopService.queryListByCollectV1(loginUserId,query.getCommunityId(), MallShopTypeDictEnum.QQMALLSHOP.getCode(),
					MallShopStyleDictEnum.LOCAL.getCode(), MallShopAuditStatusDictEnum.NORMAL.getCode(),query.getX(),query.getY());
		}
		// 先查出我收藏的店铺 end
		MallShopQuery shopQuery = new MallShopQuery();
		shopQuery.setCommunityId(query.getCommunityId());
		shopQuery.setX(query.getX());
		shopQuery.setY(query.getY());
		shopQuery.setType(MallShopTypeDictEnum.QQMALLSHOP.getCode());
		shopQuery.setStyle(MallShopStyleDictEnum.LOCAL.getCode());
		shopQuery.setAuditStatus(MallShopAuditStatusDictEnum.NORMAL.getCode());
		if (collectShops != null && !collectShops.isEmpty()) {
			List<Integer> notInIds = new ArrayList<Integer>(collectShops.size());
			for (MallShopVo t : collectShops) {
				notInIds.add(t.getId());
			}
			shopQuery.setNotInIds(notInIds);
		}
		if(query.getSortType().equalsIgnoreCase(MallSortTypeEnum.SALES.getCode())){//按销量
			pageKit = new PageKit(1, 8, new PageOrder(Direction.DESC, "saleOrderCount"),new PageOrder(Direction.ASC, "hasRecommend"));
		}else{//按距离
			pageKit = new PageKit(1, 8, new PageOrder(Direction.ASC, "distance"),new PageOrder(Direction.ASC, "hasRecommend"));
		}
		
		Page<MallShopVo> comShopPage = mallShopService.queryDistancePage(shopQuery, pageKit);
		if (comShopPage != null && comShopPage.getContent() != null) {
			List<MallShopVo> temComList = comShopPage.getContent();
			/*if (collectShops != null && !collectShops.isEmpty()) {
				temComList.addAll(0, collectShops);
			}*/
			
			for (MallShopVo t : temComList) {
				t.setRemarks1("已售" + t.getSaleOrderCount().intValue() + "单");
				t.setRemarks2(t.getDeliveryValue() + "元起送");
			}
		}
		if (collectShops != null && !collectShops.isEmpty()) {
			for (MallShopVo t : collectShops) {
				t.setRemarks1("已售" + t.getSaleOrderCount().intValue() + "单");
				t.setRemarks2(t.getDeliveryValue() + "元起送");
			}
			result.put("collectShops", collectShops);
		}
		result.put("adList", adList);
		result.put("productList", productList);
		result.put("neiShopList", neiShopList);
		result.put("comShopPage", comShopPage);
		return result;
	}

	/**
	 * 发现首页的周边店铺列表
	 * 
	 * @param query
	 * @return
	 */
	@RequestMapping(value = "/discoverList")
	@ResponseBody
	public Result discoverList(@RequestBody MallShopQuery query) {
		// 先查出我收藏的店铺
		Integer loginUserId = super.getUserId();
		List<MallShopVo> collectShops = null;
		if (loginUserId != null) {
			collectShops = mallShopService.queryListByCollectV1(loginUserId,query.getCommunityId(), MallShopTypeDictEnum.QQMALLSHOP.getCode(),
					MallShopStyleDictEnum.LOCAL.getCode(), MallShopAuditStatusDictEnum.NORMAL.getCode(),query.getX(),query.getY());
		}
		if (collectShops != null && !collectShops.isEmpty()) {
			List<Integer> notInIds = new ArrayList<Integer>(collectShops.size());
			for (MallShopVo t : collectShops) {
				notInIds.add(t.getId());
			}
			query.setNotInIds(notInIds);
		}
		// 先查出我收藏的店铺 end
		Result result = new Result();
		query.setType(MallShopTypeDictEnum.QQMALLSHOP.getCode());
		query.setStyle(MallShopStyleDictEnum.LOCAL.getCode());
		query.setAuditStatus(MallShopAuditStatusDictEnum.NORMAL.getCode());
		Pageable pageKit = null;
		if(query.getSortType().equalsIgnoreCase(MallSortTypeEnum.SALES.getCode())){//按销量
			pageKit = new PageKit(query.getPage(), 8, new PageOrder(Direction.DESC, "saleOrderCount"),new PageOrder(Direction.ASC, "hasRecommend"));
		}else{//按距离
			pageKit = new PageKit(query.getPage(), 8, new PageOrder(Direction.ASC, "distance"),new PageOrder(Direction.ASC, "hasRecommend"));
		}
		
		Page<MallShopVo> comShopPage = mallShopService.queryDistancePage(query, pageKit);
		if (comShopPage != null && comShopPage.getContent() != null) {
			List<MallShopVo> temComList = comShopPage.getContent();
			for (MallShopVo t : temComList) {
				t.setRemarks1("已售" + t.getSaleOrderCount().intValue() + "单");
				t.setRemarks2(t.getDeliveryValue() + "起送");
			}
		}
		result.put("comShopPage", comShopPage);
		return result;
	}

	/**
	 * 邻里店列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "/neiShopPage")
	@ResponseBody
	public Result neiShopPage(@RequestBody MallShopQuery query) {
		Result result = new Result();
		// 先查出我收藏的店铺
		Integer loginUserId = super.getUserId();
		List<MallShopVo> collectShops = null;
		if (loginUserId != null) {
			collectShops = mallShopService.queryListByCollect(loginUserId,query.getCommunityId(), MallShopTypeDictEnum.QQMALLSHOP.getCode(),
					MallShopStyleDictEnum.VIRTUAL.getCode(), MallShopAuditStatusDictEnum.NORMAL.getCode());
		}
		if (collectShops != null && !collectShops.isEmpty()) {
			List<Integer> notInIds = new ArrayList<Integer>(collectShops.size());
			for (MallShopVo t : collectShops) {
				notInIds.add(t.getId());
			}
			query.setNotInIds(notInIds);
		}
		// 先查出我收藏的店铺 end
		if (query.getPage().intValue() == 1) {
			// 广告
			List<AdBaseVo> adList = adBaseService.getAdListByType(AdTypeDictEnum.DISCOVER,query.getCommunityId());
			result.put("adList", adList);
			result = getMallShopCategoryVoList(result, query.getCommunityId(), MallShopStyleDictEnum.VIRTUAL.getCode());
		}
		query.setType(MallShopTypeDictEnum.QQMALLSHOP.getCode());
		query.setStyle(MallShopStyleDictEnum.VIRTUAL.getCode());
		query.setAuditStatus(MallShopAuditStatusDictEnum.NORMAL.getCode());
		Pageable pageKit = new PageKit(query.getPage(), 30, new PageOrder(Direction.DESC, "id"));
		Page<MallShopVo> page = mallShopService.queryPageList(query, pageKit);
		if (page != null && page.getContent() != null) {
			
			// 把我收藏的店铺显示在前面
			/*if (query.getPage().intValue() == 1 && collectShops != null && !collectShops.isEmpty()) {
				page.getContent().addAll(0, collectShops);
			}*/
			List<MallShopVo> shopList = page.getContent();
			pageKit = new PageKit(1, 3, new PageOrder(Direction.DESC, "id"));
			for (MallShopVo vo : shopList) {
				MallProductQuery productQuery = new MallProductQuery();
				productQuery = new MallProductQuery();
				productQuery.setShopId(vo.getId());
				productQuery.setStatus(MallProductStatusDictEnum.ONSHELF.getCode());
				List<MallProductVo> mallProductVoList = mallProductService.queryList(productQuery, pageKit);
				vo.setMallProductVoList(
						mallProductVoList == null ? new ArrayList<MallProductVo>(3) : mallProductVoList);
				int c = 3 - vo.getMallProductVoList().size();
				for (int i = 0; i < c; i++) {
					MallProductVo t = new MallProductVo();
					vo.getMallProductVoList().add(t);
				}
			}
		}
		if (collectShops != null && !collectShops.isEmpty()) {
			pageKit = new PageKit(1, 3, new PageOrder(Direction.DESC, "id"));
			for (MallShopVo vo : collectShops) {
				MallProductQuery productQuery = new MallProductQuery();
				productQuery = new MallProductQuery();
				productQuery.setShopId(vo.getId());
				productQuery.setStatus(MallProductStatusDictEnum.ONSHELF.getCode());
				List<MallProductVo> mallProductVoList = mallProductService.queryList(productQuery, pageKit);
				vo.setMallProductVoList(
						mallProductVoList == null ? new ArrayList<MallProductVo>(3) : mallProductVoList);
				int c = 3 - vo.getMallProductVoList().size();
				for (int i = 0; i < c; i++) {
					MallProductVo t = new MallProductVo();
					vo.getMallProductVoList().add(t);
				}
			}
			result.put("collectShops", collectShops);
		}
		result.put("page", page);
		return result;
	}

	/**
	 * 社区店铺列表
	 * 
	 * @param query
	 * @return
	 */
	@RequestMapping(value = "/comShopPage")
	@ResponseBody
	public Result comShopPage(@RequestBody MallShopQuery query) {
		Result result = new Result();
		// 先查出我收藏的店铺
		Integer loginUserId = super.getUserId();
		List<MallShopVo> collectShops = null;
		if (loginUserId != null) {
			collectShops = mallShopService.queryListByCollect(loginUserId, query.getCommunityId(),MallShopTypeDictEnum.QQMALLSHOP.getCode(),
					MallShopStyleDictEnum.LOCAL.getCode(), MallShopAuditStatusDictEnum.NORMAL.getCode());
		}
		if (collectShops != null && !collectShops.isEmpty()) {
			List<Integer> notInIds = new ArrayList<Integer>(collectShops.size());
			for (MallShopVo t : collectShops) {
				notInIds.add(t.getId());
			}
			query.setNotInIds(notInIds);
		}
		// 先查出我收藏的店铺 end
		query.setType(MallShopTypeDictEnum.QQMALLSHOP.getCode());
		query.setStyle(MallShopStyleDictEnum.LOCAL.getCode());
		query.setAuditStatus(MallShopAuditStatusDictEnum.NORMAL.getCode());
		Pageable pageKit = new PageKit(query.getPage(), 30, new PageOrder(Direction.DESC, "id"));
		Page<MallShopVo> page = mallShopService.queryPageList(query, pageKit);
		if (query.getPage().intValue() == 1) {
			result = getMallShopCategoryVoList(result, query.getCommunityId(), MallShopStyleDictEnum.LOCAL.getCode());
			int t = 0;
			if (collectShops != null && !collectShops.isEmpty()) {
				for (int i = 0; i < 4; i++) {
					if (collectShops.size() > i) {
						MallShopVo vo = collectShops.get(i);
						MallProductQuery productQuery = new MallProductQuery();
						productQuery = new MallProductQuery();
						productQuery.setShopId(vo.getId());
						productQuery.setStatus(MallProductStatusDictEnum.ONSHELF.getCode());
						List<MallProductVo> mallProductVoList = mallProductService.queryList(productQuery, pageKit);
						vo.setMallProductVoList(mallProductVoList);
						t++;
					}
				}
				result.put("collectShops", collectShops);
			}
			pageKit = new PageKit(1, 8, new PageOrder(Direction.DESC, "id"));
			for (int i = 0; i < (4-t); i++) {
				List<MallShopVo> shopList = page.getContent();
				if (shopList.size() > i) {
					MallShopVo vo = shopList.get(i);
					MallProductQuery productQuery = new MallProductQuery();
					productQuery = new MallProductQuery();
					productQuery.setShopId(vo.getId());
					productQuery.setStatus(MallProductStatusDictEnum.ONSHELF.getCode());
					List<MallProductVo> mallProductVoList = mallProductService.queryList(productQuery, pageKit);
					vo.setMallProductVoList(mallProductVoList);
				}
			}
			// 广告
			List<AdBaseVo> adList = adBaseService.getAdListByType(AdTypeDictEnum.DISCOVER,query.getCommunityId());
			result.put("adList", adList);
		}
		result.put("page", page);
		return result;
	}

	/**
	 * 列表页面初始化店铺分类，获取一级分类，和第一个一级分类的子分类
	 * 
	 * @param result
	 * @return
	 */
	public Result getMallShopCategoryVoList(Result result, Integer communityId, Integer style) {
		List<MallShopCategoryVo> firstCategory = mallShopCategoryService.findParentByShop(communityId, style);
		if (firstCategory == null) {
			firstCategory = new ArrayList<MallShopCategoryVo>();
		}
		MallShopCategoryVo all = new MallShopCategoryVo();
		all.setCategoryId("");
		all.setName("全部");
		all.setSelected("1");
		firstCategory.add(0, all);
		result.put("firstCategory", firstCategory);
		return result;
	}

	@RequestMapping(value = "/buyDetail")
	@ResponseBody
	public Result buyDetail(@RequestBody MallProductQuery param) {
		Result result = new Result();
		MallShopVo mallShop = null;
		if (param == null) {
			result.addErr("参数为空");
		} else if (param.getShopId() == null) {
			result.addErr("店铺id为空");
		} else {
			mallShop = mallShopService.getById(param.getShopId());
			if (mallShop == null) {
				result.addErr("店铺不存在");
			} else if (mallShop.getAuditStatus().intValue() != MallShopAuditStatusDictEnum.NORMAL.getCode()) {
				result.addErr("店铺未审核");
			}
		}
		if (result.checkForm()) {
			UserBaseVo userBase = userBaseService.getById(mallShop.getCreatorId());
			if (userBase != null) {
				mallShop.setUserNick(userBase.getNick());
			}
			if (super.getUserId() == null) {
				mallShop.setHasCollect(false);
			} else {
				mallShop.setHasCollect(pubConcernService.isConcernedByUser(mallShop.getId().intValue(),
						BizzLevel2Enum.QSHOP_SHOP.getCode(), super.getUserId()));
			}
			param.setStatus(MallProductStatusDictEnum.ONSHELF.getCode());
			Page<MallProductVo> proPage = null;
			// 推荐商品
			param.setIsRecommend(YesNoEnum.YES.getCode());
			List<MallProductVo> recommendProList = mallProductService.queryList(param);
			// 推荐商品end
			List<MallCategoryVo> categoryList = mallProductService.queryShopSellCategory(mallShop.getId());
			if (categoryList == null) {
				if (recommendProList != null && !recommendProList.isEmpty()) {
					categoryList = new ArrayList<MallCategoryVo>(2);
				} else {
					categoryList = new ArrayList<MallCategoryVo>(1);
				}
			}
			MallCategoryVo all = new MallCategoryVo();
			all.setCategoryId("");
			all.setName("全部");
			all.setSelected("1");
			categoryList.add(0, all);
			if (recommendProList != null && !recommendProList.isEmpty()) {
				MallCategoryVo recommend = new MallCategoryVo();
				recommend.setCategoryId("0");
				recommend.setName("推荐商品");
				recommend.setSelected("1");
				all.setSelected("0");
				categoryList.add(1, recommend);
			} else {
				// 没有推荐商品的话
				param.setIsRecommend(null);
				Pageable pageKit = new PageKit(param.getPage(), param.getSize(), new PageOrder(Direction.DESC, "id"));
				proPage = mallProductService.queryPageList(param, pageKit);
			}
			Integer buyUserId = super.getUserId();
			int cartTotalProduct = 0;
			BigDecimal cartTotalPrice = BigDecimal.ZERO;
			if (buyUserId != null && mallShop.getId() != null) {
				cartTotalProduct = mallCartService.getTotalQuantity(buyUserId, mallShop.getId());
				cartTotalPrice = mallCartService.getTotalPrice(buyUserId, mallShop.getId());
			}
			result.put("recommendProList", recommendProList);
			result.put("categoryList", categoryList);
			result.put("proPage", proPage);
			result.put("mallShop", mallShop);
			result.put("cartTotalProduct", cartTotalProduct);
			result.put("cartTotalPrice", cartTotalPrice);
		}
		return result;
	}

	@RequestMapping(value = "/buyDetailPro")
	@ResponseBody
	public Result buyDetailPro(@RequestBody MallProductQuery param) {
		Result result = new Result();
		if (param == null) {
			result.addErr("参数为空");
		} else if (param.getShopId() == null) {
			result.addErr("店铺id为空");
		}
		if (result.checkForm()) {
			if ("0".equals(param.getCategoryId())) { // 0在这里表示查推荐商品
				param.setIsRecommend(YesNoEnum.YES.getCode());
				param.setCategoryId(null);
			}
			param.setStatus(MallProductStatusDictEnum.ONSHELF.getCode());
			Pageable pageKit = new PageKit(param.getPage(), param.getSize(), new PageOrder(Direction.DESC, "id"));
			Page<MallProductVo> proPage = mallProductService.queryPageList(param, pageKit);
			result.put("proPage", proPage);
		}
		return result;
	}

	/**
	 * 查询她的店铺id--用于跳转到她的店铺详情
	 * 
	 * @param userQ
	 * @return
	 */
	@RequestMapping(value = "/herShopId")
	@ResponseBody
	public Result herShopId(@RequestBody UserBaseQuery userQ) {
		Result result = new Result();
		Integer userId = userQ.getId();
		if (userId == null) {
			result.addErr("用户id为空");
			return result;
		}
		MallShopVo mallShop = mallShopService.getByCreatorId(userId);
		if (mallShop == null) {
			result.put("hasShop", 0);
		} else {
			result.put("hasShop", 1);
			result.put("shopId", mallShop.getId());
		}
		return result;
	}

	/**
	 * 店铺（搜索）
	 * 
	 * @param mallShopQuery
	 *            {page:page}
	 * @return
	 */
	@RequestMapping(value = "/search")
	@ResponseBody
	public Result search(@RequestBody MallShopQuery query) {
		Result result = new Result();
		query.setAuditStatus(MallShopAuditStatusDictEnum.NORMAL.getCode());
		query.setStatus(MallShopStatusDictEnum.NORMAL.getCode());
		Pageable pageKit = new PageKit(query.getPage(), new PageOrder(Direction.DESC, "id"));
		Page<MallShopVo> page = mallShopService.queryPageList(query, pageKit);
		result.put("page", page);
		return result;
	}
	
	/**
	 * 修改-圈币结算比例
	 * @param mallShopVo
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/updateQuanPayRate")
	@ResponseBody
	public Result updateQuanPayRate(@RequestBody MallShopVo mallShopVo) {
		Result result = new Result();
		MallShopVo old = null;
		if (mallShopVo == null) {
			result.addErr("参数不能为空");
		} else if (mallShopVo.getId() == null) {
			result.addErr("参数id不能为空");
		} else {
			old = mallShopService.getById(mallShopVo.getId());
			if (old == null) {
				result.addErr("店铺不存在");
			} else {
				if (mallShopVo.getQuanPayRate() == null) {
					result.addErr("请输入圈币结算比例");
				}
				if (mallShopVo.getQuanPayRate().doubleValue() >1 || mallShopVo.getQuanPayRate().doubleValue()<0) {
					result.addErr("圈币结算比例必须在0到1之间");
				}
			}
		}
		if (result.checkForm()) {
			MallShop entity = new MallShop();
			entity.setId(mallShopVo.getId());
			if (mallShopVo.getQuanPayRate() == null || mallShopVo.getQuanPayRate().compareTo(BigDecimal.ZERO) < 1) {
				entity.setQuanPayRate(BigDecimal.ZERO);
			} else {
				entity.setQuanPayRate(mallShopVo.getQuanPayRate());
			}
			mallShopService.updateById(entity);
			result.put("mallShop", mallShopVo);
		}
		return result;
	}
	
	/**
	 * 获取首页货架数据
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/getQshop")
	@ResponseBody
	public Result getQshop(@RequestBody Map<String, String> map) {
		Result result = new Result();
		Integer communityId = null;
		if (StringUtil.isNotBlank(map.get("communityId"))) {
			communityId = Integer.valueOf(map.get("communityId"));
		}
		// 便利店信息
		MallShopQuery query = new MallShopQuery();
		query.setCommunityId(communityId);
		query.setStyle(MallShopStyleDictEnum.QQSTORE.getCode());
		query.setStatus(MallShopStatusDictEnum.NORMAL.getCode());
		query.setAuditStatus(MallShopAuditStatusDictEnum.NORMAL.getCode());
		List<MallShopVo> list = mallShopService.queryList(query);
		if(null!=list&&list.size()>0){
			result.put("qShop", list.get(0));
		}
		return result;
	}
}
